#!/usr/bin/env bash

DRIVER_PATH="${TRAVIS_BUILD_DIR}/vic/drivers/classic/"
DRIVER_EXE="${DRIVER_PATH}vic_classic.exe"
SAMPLES_PATH="${TRAVIS_BUILD_DIR}/samples/"

function vic_before_install {
    echo vic_before_install
    echo $PATH
    if [ ! -z "$USE_CC" ]; then
        echo "export CC=$USE_CC"
        export CC=$USE_CC;
        $CC --version
    fi
    if [ "$TRAVIS_OS_NAME" = "osx" ]; then
        echo "Testing on osx"
		if [ ! -z "$BREW_TAPS" ]; then
			for tap in $BREW_TAPS
			do
				brew tap $tap
			done
		fi
        if [ ! -z "$BREW_INSTALLS" ]; then
            brew update
            for pkg in $BREW_INSTALLS
            do
                brew outdated $pkg || brew upgrade $pkg || brew install $pkg
                brew link $pkg
            done
        fi
    fi
}

function vic_install {
    echo vic_install
    $CC --version
    which $CC
    type $CC
    make full -C $DRIVER_PATH
}

function vic_before_script {
    echo vic_before_script
    if [ ! -f $DRIVER_EXE ]; then
        echo "Executable (${DRIVER_EXE}) not found!"
        exit 1
    fi

    echo "Getting sample data"
    bash ${SAMPLES_PATH}/get_sample_data.bash
}

function vic_script {
    echo vic_script
    $DRIVER_EXE -v
    $DRIVER_EXE -o

    # Run test package
    ./tests/run_tests.py unit examples system \
        --classic=${DRIVER_EXE} \
        --data_dir=${SAMPLES_PATH}/data
}

function vic_after_success {
    echo vic_after_success
    echo "Success!"
}

function vic_after_failure {
    echo vic_after_failure
    echo "Test failed -- please review the log"
}
